package com.cgwx.tmservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cgwx.tmservice.config.mybatisplus.RequestDynamicTableNameHelper;
import com.cgwx.tmservice.entity.bean.TmParamConfigIdToTmCode;
import com.cgwx.tmservice.entity.tm.TmInfoHisDate;
import com.cgwx.tmservice.entity.vo.TmReplayDataVo;
import com.cgwx.tmservice.mapper.TmInfoHisDateMapper;
import com.cgwx.tmservice.service.TmInfoHisDateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 遥测解析记录表 服务实现类
 * </p>
 *
 * @author GaoWei
 * @since 2023-08-31
 */
@Service
public class TmInfoHisDateServiceImpl extends ServiceImpl<TmInfoHisDateMapper, TmInfoHisDate> implements TmInfoHisDateService {

    @Autowired
    private TmInfoHisDateMapper tmInfoHisDateMapper;

//    创建每日遥测解析记录表
    @Override
    public int createTable(String tmInfoHisDateTableName) {
        return tmInfoHisDateMapper.createTable(tmInfoHisDateTableName);
    }

//    在每日遥测解析记录表中批量插入数据
    @Override
    public void saveTmInfoHisDateBatch(String tmInfoHisTableName, ArrayList<TmInfoHisDate> tmInfoHisDateBeanList) {
        if(StringUtils.isNotBlank(tmInfoHisTableName)){
            RequestDynamicTableNameHelper.setRequestData(tmInfoHisTableName);
        }
        tmInfoHisDateMapper.insertBatchSomeColumn(tmInfoHisDateBeanList);
    }

//    导出解析记录
    @Override
    public List<TmInfoHisDate> getTmHisReplayList(TmReplayDataVo tmReplayDataVo) {
//        设置待查询的表名
        String tmInfoHisDateTableName = "tm_info_his_" + tmReplayDataVo.getReplayDate();
        if(StringUtils.isNotBlank(tmInfoHisDateTableName)){
            RequestDynamicTableNameHelper.setRequestData(tmInfoHisDateTableName);
        }
        QueryWrapper<TmInfoHisDate> queryWrapper = new QueryWrapper<>();
//        回放时间，需要加日期
        String startTime = tmReplayDataVo.getReplayDate().substring(0,4) + "-" + tmReplayDataVo.getReplayDate().substring(4,6)
                           + "-" + tmReplayDataVo.getReplayDate().substring(6,8) + " " + tmReplayDataVo.getReplayStartTime();
        String endTime = tmReplayDataVo.getReplayDate().substring(0,4) + "-" + tmReplayDataVo.getReplayDate().substring(4,6)
                         + "-" + tmReplayDataVo.getReplayDate().substring(6,8) + " " + tmReplayDataVo.getReplayEndTime();
        queryWrapper.between("gmt_create",startTime,endTime);
//        回放通路
        queryWrapper.eq("dataChannel",tmReplayDataVo.getReplayChannel());
//        根据id得到遥测代号
        ArrayList<String> replayTmCodeList = new ArrayList<>();
        ArrayList<String> replayTmIdList = tmReplayDataVo.getReplayTmList();
        for (String tmId : replayTmIdList) {
            String tmCode = TmParamConfigIdToTmCode.TM_PARAM_ID_TMCODE.get(tmId);
            replayTmCodeList.add(tmCode);
        }
        queryWrapper.in("tm_code",replayTmCodeList);
        return tmInfoHisDateMapper.selectList(queryWrapper);
    }
}
